<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12 0012
 * Time: 17:06
 */

namespace app\home\controller;


use app\model\model\Ad;
use app\model\model\Application;
use app\model\model\Application_type;
use app\model\model\Seo;
use think\Request;

class Applications extends Base
{
    protected $_Seo;
    protected $_Ad;
    protected $_App;
    protected $_Type;

    public function __construct(Request $request = null){
        parent::__construct($request);

        $this->_Ad = new Ad();
        $this->_Seo = new Seo();
        $this->_App = new Application();
        $this->_Type = new Application_type();
    }
    public function index(){
        //SEO
        $seo = $this->_Seo->getOne(['id'=>1]);
        $this->assign('seo',$seo);
        //banner
        $banner = $this->_Ad->getListHome(['type_id'=>15,'flag'=>1]);
        $this->assign('banner',$banner);

        $type = $this->_Type->getListSelect();
        $this->assign('type',$type);
        $application = $this->_App->getListHome(['flag'=>1]);
        foreach ($application as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['img_url'] = json_decode($v['img_url'],true);
        }
        $this->assign('application',$application);
        return $this->fetch();
    }

    public function getApplication(){
        $type1 = input('type1')?input('type1'):false;
        $type2 = input('type2')?input('type2'):false;
        $type3 = input('type3')?input('type3'):false;
        $condition = [];
        if(!empty($type1)){
            $condition['type1'] = $type1;
        }
        if(!empty($type2)){
            $condition['type2'] = $type2;
        }
        if(!empty($type3)){
            $condition['type3'] = $type3;
        }
        $condition['flag'] =1;
        $application = $this->_App->getListSelect($condition);
          foreach ($application as $val){
              if(json_decode($val['img_url'])){
                  $val['img_url_site'] = json_decode($val['img_url'])[0];
              }

              $val['uname'] = str_replace(" ","-",$val['name']);
          }
        echo json_echo_msg(200, 'chenggong', $application);exit();
    }
    public function view(){

        $c = $_SERVER['REQUEST_URI'];
        $id = explode(".",$c);
        $id = explode("-",$id[0]);
        $id = end($id);
        $data = $this->_App->getOne($id);
        if(empty($data)){
            $this->error('没有数据');
        }
        $data = $data->toArray();
        $data['img_url_arr'] = json_decode($data['img_url'], true);
        $data['scene_tags_arr'] = explode(',', $data['scene_tags']);
        $data['scene_img_arr'] = json_decode($data['scene_img'], true);
        $data['mark_one_arr'] = json_decode($data['mark_one'], true);


        $data['mark_one_arr']['tags_arr'] = explode(',',$data['mark_one_arr']['tags']);
        $data['mark_two_arr'] = json_decode($data['mark_two'], true);
        $data['mark_two_arr']['tags_arr'] = explode(',',$data['mark_two_arr']['tags']);
        $data['advantage_arr'] = json_decode($data['advantage_ids'], true);
        if($data['advantage_arr']){
            $advantage_model = new \app\model\model\Advantage();
            $data['advantage_arr'] = $advantage_model->getListHome(['id'=>['in', $data['advantage_arr']]]);
        }
        $data['sample_ids_arr'] = json_decode($data['sample_ids'], true);
        if($data['sample_ids_arr']){
            $sample_model = new \app\model\model\Sample();
            $sample_list = $sample_model->getListSelect(['id'=>['in', $data['sample_ids_arr']]]);
            $data['sample_ids_arr'] = $sample_list;
        }

        $data['product_ids_arr'] = json_decode($data['product_ids'], true);
        $data['product_list_arr'] = [];
        if($data['product_ids_arr']){
            $product_model = new \app\model\model\Product();
            $data['product_list_arr'] = $product_model->getListSelect(['id'=>['in', $data['product_ids_arr']]]);
            foreach ($data['product_list_arr'] as $val){
                $val['img_url_site'] = json_decode($val['img'], true)[0];
                $val['uname'] = str_replace(" ","-",$val['name']);
            }
        }
        $this->assign('data',$data);
        return $this->fetch();
    }
}